#!/bin/bash
current_dir=`pwd`
current_user=$1
#svn repo configuration
repo_name=".repo"
wcopy_name=".vsync_workingcopy"

echo "------vSync Configuration------"
echo "Stopping svnserve..."
/etc/init.d/svnserve stop
echo "svnserve stopped!"
mkdir -p $HOME/$repo_name
svnadmin create $HOME/$repo_name
cp $current_dir/conf/svnserve/repository/* $HOME/$repo_name/conf/
cp $current_dir/conf/svnserve/svnserve /etc/init.d/
echo "Starting svnserve..."
/etc/init.d/svnserve start

#vsync settings file
mkdir -p /etc/vsync
cp $current_dir/conf/settings.xml /etc/vsync
#vsync run script
cp $current_dir/bin/vsync /usr/bin/

#vsync.state run file
rm -f /var/run/vsync.state
touch /var/run/vsync.state
echo "" > /var/run/vsync.state
chown $current_user.$current_user /var/run/vsync.state

#vsync workingcopy configuration
svn co svn://localhost $HOME/$wcopy_name --username admin --password admintest --non-interactive 
#--no-auth-cache
#--trust-server-cert
cd $HOME/$wcopy_name
mkdir -p calendar
mkdir -p contacts
mkdir -p memos
svn add ./calendar
svn add ./contacts
svn add ./memos
svn ci --username admin -m "vsync installed" --password admintest --non-interactive
mkdir -p index
touch index/fts_index.db
chown -R $1.$1 $HOME/$wcopy_name
chown -R $1.$1 $HOME/$repo_name

echo "------vSync Web App Configuration------"
#vsyncwebapp configuration
cd $current_dir
rm -rf /var/www/versionSyncWebApp
rm -rf /var/www/media
mkdir -p /var/www/versionSyncWebApp
mkdir -p /var/www/media
cp -r ../versionSyncWebApp/src/versionSyncWebApp/* /var/www/versionSyncWebApp/
cp -r ../versionSyncWebApp/var/www/media/* /var/www/media
#setting up htpasswd
rm /etc/vsync/.htpasswd 2> /dev/null
htpasswd -c /etc/vsync/.htpasswd vsync
#setting up ssl
mkdir -p /etc/apache2/ssl
a2enmod ssl
cp ./conf/apache2/server.crt /etc/apache2/ssl/
cp ./conf/apache2/server.key /etc/apache2/ssl/
ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled/000-default-ssl
/etc/init.d/apache2 restart
